#ifndef _MESSAGEDISPATCHER_H
#define _MESSAGEDISPATCHER_H

#include "utils.h"

#include <map>
#include <memory>
#include <mqueue.h>
#include <mutex>
#include <vector>

namespace droneswarm
{
    constexpr int MaxReadCount = 20;
    class MessageDispatcher
    {
    public:
        MessageDispatcher();
        ~MessageDispatcher();
        void initDispatcher( mqd_t mq )
        {
            std::cout << "消息分发器初始化成功" << std::endl;
            mq_ = mq;
        }
        void registerDispatcher( int fd );
        void cancelFdFromDispatcher( int fd );
        std::vector< BlockOpMsg > dp_receive( int fd );
        bool dp_send( int fd, BlockOpMsg bom, int maxTires );
        
    private:
        bool readToBmsgArray();

    private:
        // std::map< int, std::shared_ptr< BlockOpMsg > > fdDispatcher_;  //这是存在fd和消息的映射关系的容器
        std::map< int, std::vector< BlockOpMsg > > fdDispatcher_;
        BlockOpMsg bmsgs_[ 20 ];
        mqd_t mq_;
        std::mutex dispatcherMutex_;
    };
}  // namespace droneswarm

#endif